﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace App.MVC.Extend
{
    public class CookieHelper
    {

        public static bool DelAllCookie()
        {
            try
            {
                foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
                {
                    var cookie = HttpContext.Current.Request.Cookies[cookiename];
                    if (cookie == null) continue;
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }

                return true;
            }
            catch
            {
                throw new Exception("DelAllCookie Error");
            }
        }

        /// <summary>
        /// 清除指定Cookie
        /// </summary>
        /// <param name="cookiename">cookiename</param>
        public static void ClearCookie(string cookiename)
        {
            var cookie = HttpContext.Current.Request.Cookies[cookiename];
            if (cookie == null) return;
            cookie.Expires = DateTime.Now.AddYears(-3);
#if (DEBUG)
            //LogInfo.Info("清除Cookie：" + cookiename);
#endif
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取指定Cookie值
        /// </summary>
        /// <param name="cookiename">cookiename</param>
        /// <returns></returns>
        public static string GetCookieValue(string cookiename)
        {
            var cookie = HttpContext.Current.Request.Cookies[cookiename];
            var str = cookie != null ? HttpUtility.UrlDecode(cookie.Value) : string.Empty;
#if (DEBUG)
            //LogInfo.Info("获取Cookie名：【" + cookiename + "】的值为：【" + str + "】");
#endif
            return str;
        }

        /// <summary>
        /// 获取指定的Cookie值
        /// </summary>
        /// <param name="cookieName">Cookie名</param>
        /// <param name="valueName">值</param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName, string valueName)
        {
            var cookie = HttpContext.Current.Request.Cookies[cookieName];
            string str = cookie != null ? HttpUtility.UrlDecode(cookie[valueName]) : string.Empty;
#if (DEBUG)
            //LogInfo.Info("获取Cookie名：【" + cookieName + ":" + valueName + "】的值为：【" + str + "】");
#endif
            return str;
        }

        /// <summary>
        /// 添加一个Cookie（7天）
        /// </summary>
        /// <param name="cookiename"></param>
        /// <param name="cookievalue"></param>
        public static void SetCookie(string cookiename, string cookievalue)
        {
            SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(7));
        }

        /// <summary>
        /// 添加一个Cookie
        /// </summary>
        /// <param name="cookiename">cookie名</param>
        /// <param name="cookievalue">cookie值</param>
        /// <param name="expires">过期时间 DateTime</param>
        public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
        {
            var cookie = new HttpCookie(cookiename)
            {
                Value = HttpUtility.UrlEncode(cookievalue)
                //Expires = expires
            };
#if (DEBUG)
            //LogInfo.Info("设置Cookie名：【" + cookiename + "】的值为：【" + cookievalue + "】" + "时间为：【" + expires + "】");
#endif
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}